package model.jena;

import java.util.List;

import model.jena.properties.FileStore;
import status.FileItemStatus;
import utility.StringUtility;

import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sparql.util.StringUtils;

import filestorage.FileStoreItem;

public class ModelBuilder {

	
	public static Model buildModel(List<FileStoreItem> listOfItems){
		Model model = ModelFactory.createDefaultModel();
		for(int i = 0; i < listOfItems.size(); i++){
			FileStoreItem item = listOfItems.get(i) ;
			FileItemStatus status = item.getStatus();
			Resource fileStatusResource = model.createResource("http://csi.ucd.ie/auto/"+item.getName());
			fileStatusResource.addProperty(FileStore.AVG_SERVICE_TIME, ""+status.getAverageServiceTime());
			fileStatusResource.addProperty(FileStore.FILENAME,item.getName());
			fileStatusResource.addProperty(FileStore.QUICKEST_SERVICE_TIME,""+status.getQuickestServiceTime());
			fileStatusResource.addProperty(FileStore.SLOWEST_SERVICE_TIME,""+status.getSlowestServiceTime());
			fileStatusResource.addProperty(FileStore.TOTAL_NUM_DOWNLOADS,""+status.getNumberOfDownloads());
		}
		Bag bagOfFiles = model.createBag();
		
		StmtIterator iter = model.listStatements(new SimpleSelector(null,FileStore.FILENAME,(RDFNode)null){

			@Override
			public boolean selects(Statement s) {
				return super.selects(s);
			}
		});
        while (iter.hasNext()) {
        	bagOfFiles.add( iter.nextStatement().getSubject());
        }
		return model;
	}
}
